package com.cupster.func_db_greendao.crud;

import android.content.Context;
import android.util.Log;

import com.cupster.func_db_greendao.DaoManager;
import com.cupster.func_db_greendao.auto.NewsDao;
import com.cupster.func_db_greendao.entity.News;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class NewsDaoHelper {

    private static final String TAG = "NewsDaoHelper";
    private DaoManager mManager;

    public NewsDaoHelper(Context appContext){
        mManager = DaoManager.getInstance( appContext );
    }

    /**
     * 单条插入
     * @param news
     * @return
     */
    public boolean insert(News news){
        boolean result = mManager.getDaoSession().getNewsDao().insert(news) != -1;
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "insert: " + result +" ==> "+ news.toString());
        }
        return result;
    }

    /**
     * 多条item插入
     * @param newsList
     * @return boolean结果
     */
    public boolean insertMulti(final List<News> newsList){
        boolean result = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (News news : newsList){
                       long add_id = mManager.getDaoSession().insertOrReplace(news) ;
                        if (DaoManager.SHOW_LOG){
                            Log.d(TAG, "insert items : " + add_id +" ==> "+ news.toString());
                        }
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }


    public boolean update(News news){
        boolean result = false;
        try {
            mManager.getDaoSession().update(news);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "update: " + result +" ==> "+ news.toString());
        }
        return result;
    }


    public boolean delete(News news){
        boolean result = false;
        try {
            mManager.getDaoSession().delete(news);
            result = true;
        }catch (Exception e ){e.printStackTrace();}
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "delete: " + result +" ==> "+ news.toString());
        }
        return result;
    }


    public boolean deleteAll(){
        boolean result = false;
        try {
            mManager.getDaoSession().deleteAll(News.class);
            result = true;
        }catch (Exception e ){
            e.printStackTrace();
        }
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "delete all : " + result );
        }
        return result;
    }

    public  List <News> queryAll (){
        return mManager.getDaoSession().loadAll(News.class);
    }

    public News queryById(long _id){
        return mManager.getDaoSession().load(News.class ,_id);
    }

    public List<News> queryBySQL(String sql , String[] conditions){
        return mManager.getDaoSession().queryRaw(News.class ,sql ,conditions);
    }

    /**
     * 使用queryBuilder 条件查询
     * @return
     */
    public List<News> queryByQueryBuilder(long id){
        QueryBuilder<News> queryBuilder = mManager.getDaoSession().queryBuilder(News.class);
        return queryBuilder.where(NewsDao.Properties._id.eq(id)).list();
    }

    /**
     * 查询已收藏文章
     * @param isCollected 0=false 1=true
     * @return
     */
    public List<News> queryByQueryBuilder(int isCollected){
        QueryBuilder<News> queryBuilder = mManager.getDaoSession().queryBuilder(News.class);
        return queryBuilder.where(NewsDao.Properties.IsCollected.eq(isCollected)).list();
    }

}
